<?php
	$template = strip_tags($_GET['template']);
	$filedata = read_dir('themes/'.$template.'/');
	$file = strip_tags($_GET['file']);
	$template = strip_tags($_GET['template']);
	$data = array();
	foreach ($filedata['files'] as $value) {
		if ((!preg_match('/\%/',$value)) && (!preg_match('/png/',$value)) && (!preg_match('/jpg/',$value)) && (!preg_match('/jpeg/',$value)) && (!preg_match('/gif/',$value)) && (!preg_match('/inc/',$value)))
			$data[] = $value;
	}
		
	if ($_POST['submit']) {
		$postFile = strip_tags($_POST['postFile']);
		if ($_POST['saveit']=="true") {
			$arr1 = array('\"',"\\n\\r",'\\',"&amp;","&","%","\'");
			$arr2 = array('"',"\\n","\\","&","&amp;","%","'");
			$editor = str_replace($arr1,$arr2,$_POST['editor']);
			if (file_exists('themes/'.$template.'/template/'.$file)) {
				file_put_contents('themes/'.$template.'/template/'.$postFile,$editor);
			}
		}

	}
	
	if (file_exists('themes/'.$template.'/template/'.$file))
		$readFile = @file_get_contents('themes/'.$template.'/template/'.$file);
		
?>
<script>
jQuery(document).ready(function () {
	textarea = $('#editor');
	textarea.tabOverride(true);
});
</script>

<script language="javascript">
	function notChecked() {
		var file = document.getElementById("postFile").options[document.getElementById("postFile").selectedIndex].value;
		window.document.location = 'index.php?show=<?php echo SHOW; ?>&showview=<?php echo SHOWVIEW; ?>&template=<?php echo $template; ?>&file=' + file;
	}
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="00">
  <tr>
    <td><div align="center">
      <form name="form1" method="post" action="<?php echo "index.php?show=".SHOW."&showview=".SHOWVIEW."&template=".$template."&file=".$file; ?>">
          <div align="left" style="float:left; position:absolute; margin:0 auto">
            <input name="saveit" type="hidden" id="saveit" value="true" />&nbsp;<input type="submit" name="submit" id="submit" value="<?php echo $THEMES_BUTTON_SAVE; ?>">
           &nbsp; </div>
          <div align="right" style="float:right; width:200px; margin:0 auto">
           <?php echo $THEMES_SELECT_FILE; ?> <select name="postFile" id="postFile" onchange="notChecked()">
              <option>---</option>
              <?php
		  	foreach ($data as $val) {
				echo '<option value="'.$val.'"';
				if ($file==$val)
					echo 'selected';
				echo '>'.strip_tags($val).'</option>';	
			}
		  ?>
            </select>
          </div>
          <textarea name="editor" id="editor" style="width:99%; font-size:1.4em" wrap="off" rows="25"><?php echo $readFile; ?></textarea>
      </form>
    </div></td>
  </tr>
</table>
